package com.yunfei.procedure;
import com.yunfei.api.financialCalculator.param.TongjiCalParam;
import com.yunfei.api.financialCalculator.result.TongjiCalResult;
import com.yunfei.utils.jdbc.AiiidbUtil;
import oracle.jdbc.OracleTypes;
import java.sql.*;
/**
* 调用接口
 * jcpt_func_pkg.sp_tjjsq(pair_cnt,--输入参数：几元数据，传入1或者2 字符型
* one_pair,--输入参数：第一列数组 例：1,2,3,4,5,6
* two_pair,--输入参数：第二列数组 例：1,2,3,4,5,6
* one_pair_rust,--输出参数，输出对应第一个数组的三个数字，分别为均值 样本标准差 总体标准差
* two_pair_rust,--输出参数，输出对应第二个数组的三个数字，分别为均值 样本标准差 总体标准差
* po_sp_rslt_fg );
* @Description: 统计计算器
* @Author: zhengyunfei
* @Date: 2019/2/15
* @time:16:44
*/
public class TongJiCalProcedure {
    private Connection conn;
    private CallableStatement stat;
    private ResultSet rs;
    String calSql = "call jcpt_func_pkg.sp_tjjsq(?,?,?,?,?,?)";
    /**
    * @Description: 统计计算器存储过程
    * @Author: zhengyunfei
    * @Date: 2018/4/24
    * @time:13:49
    */
    public TongjiCalResult callProcedure(AiiidbUtil aiiidbUtil, TongjiCalParam param) {
        TongjiCalResult result=new TongjiCalResult();
        try {
            String url=aiiidbUtil.getJdbcUrl();
            String driverName=aiiidbUtil.getDriveClassName();
            String username=aiiidbUtil.getJdbcUserName();
            String password=aiiidbUtil.getJdbcPassword();
            Class.forName(driverName);
            conn = DriverManager.getConnection(url, username, password);
            stat = conn.prepareCall(calSql);
            // 2个输入参数和2个输出参数
            stat.setString(1, param.getPairCnt());
            stat.setString(2, param.getOnePair());
            stat.setString(3, param.getTwoPair());
            stat.registerOutParameter(4, OracleTypes.VARCHAR);
            stat.registerOutParameter(5, OracleTypes.VARCHAR);
            stat.registerOutParameter(6, OracleTypes.VARCHAR);
            stat.execute();
            //返回结果
            String onePairRust= stat.getString(4);
            String twoPairRust= stat.getString(5);
            String poSpRsltFg= stat.getString(6);
            result.setOnePairRust(onePairRust);
            result.setTwoPairRusst(twoPairRust);
            result.setPoSpRsltFg(poSpRsltFg);
            System.out.println("统计计算器返回结果: " + result.getOnePairRust()+","+result.getTwoPairRusst()+","+result.getPoSpRsltFg());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(conn, stat, rs);
        }
        return result;
    }

    // 关闭连接
    public void close(Connection conn, CallableStatement stat, ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                rs = null;
            }
        }
        if (stat != null) {
            try {
                stat.close();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                stat = null;
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                conn = null;
            }
        }
    }

    public static void main(String[] args) {
       // new GetTouZiZheRenZhengInfoProcedure().callProcedure("20180417132146779447466");
    }
}